<template>
  <div>
    <!-- 三级分类 -->
    <Category :fetchCategoryId="fetchCategoryId"></Category>

    <!--属性与属性值 -->
    <el-card shadow="always" style="margin: 20px 0">
      <!-- 属性与属性值 -->
      <div v-show="scene == 0">
        <el-button
          type="primary"
          size="default"
          icon="el-icon-plus"
          round
          plain
          :disabled="c3 ? false : true"
          @click="addAttr"
          v-if="$isShowBtn('btn.Attr.add')"
          >添加属性</el-button
        >
        <el-table style="margin: 20px 0" border stripe :data="attrList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column prop="attrName" label="属性名称" width="140">
          </el-table-column>
          <el-table-column label="属性值" width="width">
            <!-- 使用作用域插槽 -->
            <template slot-scope="{ row, $index }">
              <el-tag
                :type="attrValue.id % 2 == 0 ? 'success' : 'info'"
                size="normal"
                v-for="(attrValue, index) in row.attrValueList"
                :key="attrValue.id"
                style="margin: 0px 10px"
                >{{ attrValue.valueName }}</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="140">
            <template slot-scope="{ row, $index }">
              <el-button
                type="warning"
                size="mini"
                icon="el-icon-edit"
                @click="updateAttr(row)"
              ></el-button>

              <el-popconfirm
                confirm-button-text="确定"
                cancel-button-text="不了"
                icon="el-icon-info"
                icon-color="red"
                :title="`你确定删除${row.attrName}吗`"
                @onConfirm="deleteAttr(row)"
              >
                <el-button
                  slot="reference"
                  type="danger"
                  size="mini"
                  icon="el-icon-delete"
                  style="margin: 0px 10px"
                ></el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 添加/修改属性 -->
      <div v-show="scene == 1">
        <el-form ref="form" :inline="true">
          <el-form-item label="属性名" size="normal">
            <el-input
              placeholder="属性名"
              size="normal"
              v-model="attrForm.attrName"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-button
          type="primary"
          size="default"
          icon="el-icon-plus"
          :disabled="attrForm.attrName ? false : true"
          @click="addAttrValue"
          >添加属性值</el-button
        >
        <el-button size="default" @click="scene = 0">取消</el-button>
        <el-table
          border
          stript
          style="margin: 20px 0"
          :data="attrForm.attrValueList"
        >
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column label="属性值名称" width="width">
            <template slot-scope="{ row, $index }">
              <!-- 编辑模式与查看模式进行切换 -->
              <el-input
                v-model="row.valueName"
                placeholder="请输入属性值"
                size="mini"
                v-if="row.visite"
                @blur="handlerBlur(row)"
                @keydown.native.enter="handlerBlur(row)"
                :ref="$index"
              ></el-input>
              <pre v-else @click="handlerClick(row, $index)">{{
                row.valueName
              }}</pre>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="width">
            <template slot-scope="{ row, $index }">
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="attrForm.attrValueList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-button
          type="primary"
          size="default"
          icon="el-icon-document-copy"
          @click="save"
          :disabled="attrForm.attrValueList.length > 0 ? false : true"
          >保存</el-button
        >
        <el-button size="default" @click="scene = 0">取消</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
import Category from "./Category";
export default {
  name: "",
  data() {
    return {
      //搜集 id值
      c1: "",
      c2: "",
      c3: "",
      attrList: [], //某一个分类的属性与属性值的k
      scene: 0, //页面切换
      attrForm: {
        attrName: "", //属性名字
        categoryId: "", //哪个的属性
        categoryLevel: 3, //三级分类
        attrValueList: [
          //   {
          // attrId: 0,
          // valueName: "",
          //   },
        ], //搜集的属性值
      },
      visite: true, //编辑与查看
    };
  },
  methods: {
    // 获取子组件传进来的id值
    fetchCategoryId(id, level) {
      //   console.log(id);
      switch (level) {
        case 1:
          this.c1 = id;
          this.c2 = "";
          this.c3 = "";

          break;
        case 2:
          this.c2 = id;
          this.c3 = "";

          break;
        default:
          this.c3 = id;

          //三个id都获取到 发送请求
          this.getAttrList();
          break;
      }
    },
    // 获取某一个分类的属性与属性值
    async getAttrList() {
      // 整理参数
      const { c1, c2, c3 } = this;

      // 发送请求
      let res = await this.$http.attr.reqAttr(c1, c2, c3);
      // console.log(res)
      if (res.code == 200) {
        this.attrList = res.data;
      }
    },
    // 点击添加跳转到添加页面
    addAttr() {
      this.scene = 1;

      // 清空属性与属性值得参数
      this.attrForm = {
        attrName: "",
        categoryId: this.c3, //哪个的属性
        categoryLevel: 3, //三级分类
        attrValueList: [],
      };
    },
    // 添加属性值
    addAttrValue() {
      this.attrForm.attrValueList.push({
        attrId: undefined,
        valueName: "",
        visite: true,
      });

      // 自动聚焦
      this.$nextTick(() => {
        this.$refs[this.attrForm.attrValueList.length - 1].focus();
      });
    },
    // 切为查看模式
    handlerBlur(row) {
      // 不能为空
      if (row.valueName.trim() == "") {
        this.$message("属性值不能为空哦");
        return;
      }
      // 不能重复出现
      let result = this.attrForm.attrValueList.find((item) => {
        if (item != row) {
          return item.valueName == row.valueName;
        }
      });
      if (result) {
        this.$message("属性值不能重复");
        this.attrForm.attrValueList.pop();
        return;
      }

      row.visite = false;
    },
    // 切为编辑模式
    handlerClick(row, index) {
      row.visite = true;

      // 自动聚焦
      this.$nextTick(() => {
        this.$refs[index].focus();
      });
    },
    // 保存按钮
    async save() {
      // 已有的属性不能发送请求
      if (
        this.attrList.find(
          (item) => item.attrName == this.attrForm.attrName && !this.attrForm.id
        )
      ) {
        this.$message("新增的属性已经存在了");
        return;
      }

      // 属性值有空不能发送请求
      let none = this.attrForm.attrValueList.find((item) => {
        return item.valueName == "";
      });
      if (none) {
        this.$message("属性值不能为空的");
        return;
      }

      // 整理参数
      this.attrForm.attrValueList = this.attrForm.attrValueList.map((item) => {
        return {
          attrId: item.attrId,
          valueName: item.valueName,
        };
      });

      //   发请求
      try {
        // 添加成功
        await this.$http.attr.reqAddOrUpdataAttr(this.attrForm);

        // 提示框信息
        this.$notify.success({
          title: "成功",
          message: this.attrForm.id ? "修改属性成功" : "添加属性成功",
        });

        // 切换场景 并获取最新的数据
        this.scene = 0;
        this.getAttrList();
      } catch (error) {
        this.$notify.error({
          title: "失败",
          message: this.attrForm.id ? "修改属性失败" : "添加属性失败",
        });
      }
    },
    // 修改属性值
    updateAttr(row) {
      this.scene = 1;
      // 浅拷贝 已有的属性与属性值
      this.attrForm = { ...row };

      //添加visite的属性
      this.attrForm.attrValueList = this.attrForm.attrValueList.map((item) => {
        return {
          ...item,
          visite: false,
        };
      });
    },
    // 点击删除属性
    async deleteAttr(row) {
      try {
        // 成功
        await this.$http.attr.reqDeleteAttr(row.id);

        // 获取最新属性数据
        this.getAttrList();
        this.$message("删除成功");

      } catch (error) {
        alert("失败啦");
      }
    },
  },
  components: {
    Category,
  },
};
</script>

<style lang="less" scoped>
</style>